using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace whereIsTheDonutGame
{
    /// <summary>
    /// Dies ist eine Spielkomponente, die IUpdateable implementiert.
    /// </summary>
    public class Hud : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game game;

        private float elapsed; // elapsed enthält immer die seit dem letzten Frame vergangenen Sekunden
        private float frameRate; // die eigentlichen FPS
        private float frames; //frames die hochgezählten Frames (erhöht sich mit jedem Frame)

        SpriteBatch spriteBatch;
        SpriteFont font;

        //Texture2D textureDonut; //hier wird Grafik Donut gespeichert
        Texture2D textureDonutKlein; //hier wird kleinere Grafik Donut gespeichert

        private int counter = 0;    //Counter für gesammelte Donuts

        private Boolean showFPS = false;    // Boolean ob FPS-Anzeige aktiviert oder nicht, Standard aus

        const int donutPosY = 420;  //Konstante Position des untersten Donuts

        public Hud(Game game)
            : base(game)
        {
            elapsed = 0.0f;
            frameRate = 0.0f;
            frames = 0.0f;
            this.game = game;
        }

        public override void Initialize()
        {

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            font = Game.Content.Load<SpriteFont>("Arial");

            //textureDonut = game.Content.Load<Texture2D>("Donut");   //Grafik Donut laden und Texture2D-Objekt zuweisen
            textureDonutKlein = game.Content.Load<Texture2D>("DonutKlein");   //Grafik Donut laden und Texture2D-Objekt zuweisen
        }

        protected override void UnloadContent()
        {
        }

        public override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                game.Exit();

            KeyboardState kbState = Keyboard.GetState();
            //zum hochzählen des Counters
            if (kbState.IsKeyDown(Keys.O))
            {
                if (counter < 10)
                {
                    counter += 1;
                }
            }
            //zum runterzählen des Counters
            if (kbState.IsKeyDown(Keys.P))
            {
                if (counter != 0)
                {
                    counter -= 1;
                }
            }

            if (kbState.IsKeyDown(Keys.F))
            {
                if (!showFPS)
                {
                    showFPS = true;
                }
                else
                {
                    showFPS = false;
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (elapsed > 1.0f)
            {
                elapsed -= 1.0f;
                frameRate = frames;
                frames = 0;
            }
            else
            {
                frames += 1;
            }

            fpsAnzeigen();

            ///*
            // * Where is the Donut? schreiben
            // **/
            //spriteBatch.Begin();
            //spriteBatch.DrawString(font, "Where is the Donut??", new Vector2(250, 100), new Color(255, 0, 0, 80));
            //spriteBatch.End();

            /*
             * Donut Counter Schrift
             **/
            spriteBatch.Begin();
            spriteBatch.DrawString(
                font,   //die Schriftart
                "Donut Counter: " + counter,    //einfacher Text
                new Vector2(540, 430),          //Position auf Bildschirm
               counter > 8 ? Color.Green : Color.OrangeRed,    //Farbe ändert sich
                0.1f,                           //Rotation um eigenen Mittelpunkt
                Vector2.Zero, //Ursprung des Sprites
                0.70f,                          //Scale
                SpriteEffects.None,             //Effekte
                0.0f                             //Tiefe der Ebene; 0 vorderste
            );
            spriteBatch.End();


            /*
             * Donut Counter Grafik  wird nur gezeichnet, wenn Counter 1 ist, dann kommt mit erhöen des Counters immer ein neuer Donut hinzu
             **/
            if (counter == 0)
            {
                //nichts zeichnen
                spriteBatch.Begin();
                spriteBatch.Draw(textureDonutKlein, new Vector2(750, donutPosY), new Color(255, 255, 255, 50));
                spriteBatch.End();
            }

            if (counter == 1)
            {
                donutDraw(donutPosY);
            }

            if (counter == 2)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
            }


            if (counter == 2)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
            }

            if (counter == 3)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
            }

            if (counter == 4)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
                donutDraw(donutPosY - 80);
            }

            if (counter == 5)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
                donutDraw(donutPosY - 80);
                donutDraw(donutPosY - 100);
            }
            if (counter == 6)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
                donutDraw(donutPosY - 80);
                donutDraw(donutPosY - 100);
                donutDraw(donutPosY - 120);
            }
            if (counter == 7)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
                donutDraw(donutPosY - 80);
                donutDraw(donutPosY - 100);
                donutDraw(donutPosY - 120);
                donutDraw(donutPosY - 140);
            }
            if (counter == 8)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
                donutDraw(donutPosY - 80);
                donutDraw(donutPosY - 100);
                donutDraw(donutPosY - 120);
                donutDraw(donutPosY - 140);
                donutDraw(donutPosY - 160);
            }
            if (counter == 9)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
                donutDraw(donutPosY - 80);
                donutDraw(donutPosY - 100);
                donutDraw(donutPosY - 120);
                donutDraw(donutPosY - 140);
                donutDraw(donutPosY - 160);
                donutDraw(donutPosY - 180);
            }
            if (counter == 10)
            {
                donutDraw(donutPosY);
                donutDraw(donutPosY - 20);
                donutDraw(donutPosY - 40);
                donutDraw(donutPosY - 60);
                donutDraw(donutPosY - 80);
                donutDraw(donutPosY - 100);
                donutDraw(donutPosY - 120);
                donutDraw(donutPosY - 140);
                donutDraw(donutPosY - 160);
                donutDraw(donutPosY - 180);
                donutDraw(donutPosY - 200);
            }
        }

        private void donutDraw(int donutPosY)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(textureDonutKlein, new Vector2(750, donutPosY), Color.White);
            spriteBatch.End();
        }

        //FPS-Anzeige einblenden
        private void fpsAnzeigen()
        {

            if (showFPS)
            {
                //FPS
                //hier wird die Schrift gezeichnet
                spriteBatch.Begin();
                spriteBatch.DrawString(font,
                    "FPS: " + frameRate.ToString("0.00"),   //Text
                    new Vector2(700, 10),            //Position auf Karte
                    Color.White,                     //Farbe
                    -0.1f,                           //Rotation um eigenen Mittelpunkt
                    Vector2.Zero,                    //Ursprung des Sprites
                    0.50f,                           //Scale
                    SpriteEffects.None,              //Effekte
                    0.0f                             //Tiefe der Ebene; 0 vorderste
                    );
                spriteBatch.End();
            }

        }
    }
}
